package org.ekstrabilet.web.beans;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.ekstrabilet.game.beans.Game;
import org.ekstrabilet.game.beans.SectorPrice;
import org.ekstrabilet.game.logic.GameLogic;
import org.ekstrabilet.logic.exceptions.StadiumException;
import org.ekstrabilet.stadium.beans.Sector;
import org.ekstrabilet.stadium.beans.Stadium;
import org.ekstrabilet.stadium.beans.Tribune;
import org.ekstrabilet.stadium.logic.SectorLogic;
import org.ekstrabilet.stadium.logic.StadiumLogic;
import org.ekstrabilet.ticket.beans.Ticket;

public class GameBean {

	private String stadium;
	private String city;
	private int year;
	private int month;
	private int day;
	
	private int hour;
	private int minute;
	
	private String teamA;
	private String teamB;
	
	private Stadium st;
	private int capacity;
	private int[] tribunes;
	
    	
	
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	public void setTribunes(int[] tribunes) {
		this.tribunes = tribunes;
	}

	public int getCapacity() {
		return capacity;
	}

	public int[] getTribunes() {
		return tribunes;
	}

	public int[] getYears() {
		
		int[] years = {2012, 2013};
		return years;
	}
	
	public int[] getMonths() {
		
		int[] months = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
		return months;
	}
	
	public int[] getDays() {
		
		int[] days = new int [31];
		for(int i = 0 ; i < days.length ; i++)
			days[i] = i;
			
		return days;
	}
	
	public int[] getHours() {
		
		int[] hours = new int [24];
		for(int i = 0 ; i < hours.length ; i++)
			hours[i] = i;
			
		return hours;
	}
	
	public int[] getMinutes() {
		
		int[] minutes = {0, 15, 30, 45};
		return minutes;
	}
	
	
	
	public int getYear() {
		return year;
	}




	public void setYear(int year) {
		this.year = year;
	}




	public int getMonth() {
		return month;
	}




	public void setMonth(int month) {
		this.month = month;
	}




	public int getDay() {
		return day;
	}




	public void setDay(int day) {
		this.day = day;
	}




	public int getHour() {
		return hour;
	}




	public void setHour(int hour) {
		this.hour = hour;
	}




	public int getMinute() {
		return minute;
	}




	public void setMinute(int minute) {
		this.minute = minute;
	}




	public String getStadium() {
		return stadium;
	}




	public void setStadium(String stadium) {
		this.stadium = stadium;
	}




	public String getCity() {
		return city;
	}




	public void setCity(String city) {
		this.city = city;
	}

	public String getTeamA() {
		return teamA;
	}




	public void setTeamA(String teamA) {
		this.teamA = teamA;
	}




	public String getTeamB() {
		return teamB;
	}




	public void setTeamB(String teamB) {
		this.teamB = teamB;
	}


	public String[] getStadiums() {
		
		return StadiumLogic.getInstance().getStadiumsArray("");
	}
	
	public String[] getCities() {
		
		return StadiumLogic.getInstance().getCitiesArray("");
	}

	public String save() {
		
		year -= 1900; // ?
		Date date = new Date(year, month, day);
		Time time = new Time(hour, minute, 0);
		
		int price = 10;
		
		try {
			
			st = StadiumLogic.getInstance().getStadium(stadium, city);
			List<SectorPrice> sectorPrices = new ArrayList<SectorPrice>();
			
			for(Tribune t : st.getTribunes())
				for(Sector s : t.getSectors())
					sectorPrices.add(new SectorPrice(s.getSectorId(), price));
			
			List<Game> games = new LinkedList<Game>();
			games.add(new Game(stadium, city, teamA, teamB, date, time, sectorPrices));
			GameLogic.getInstance().addGames(games);
			
			
		
		} catch (StadiumException e1) {
			FacesContext.getCurrentInstance().addMessage("gameForm:save", new FacesMessage(e1.getMessage()));	
		} catch (SQLException e) {
			FacesContext.getCurrentInstance().addMessage("gameForm:save", new FacesMessage(e.getMessage()));
		}
		
		
		
		return null;
	}
	
	public String next() {
		
		try {
			st = StadiumLogic.getInstance().getStadium(stadium, city);
			capacity = st.getCapacity();
			tribunes = new int[4];
			for(int i = 0 ; i < 4 ; i++)
				tribunes[i] = st.getTribune(i).getSectors().size();
			
			return "addPrices.xhtml";
			
		} catch (StadiumException e) {
			FacesContext.getCurrentInstance().addMessage("gameForm:next", new FacesMessage(e.getMessage()));	
		}
		
		return null;
	}
	

}
